package cn.Listener;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.sql.DataSource;

public class MyRequestAttributeListener implements ServletContextListener {
    @Override
    public void contextInitialized(ServletContextEvent servletContextEvent) {
        ServletContext servletContext= servletContextEvent.getServletContext();
        try {
            Context context = new InitialContext();
            DataSource source= (DataSource) context.lookup("java:comp/env/jdbc/news");
            servletContext.setAttribute("source",source);
        } catch (NamingException e) {
            e.printStackTrace();
        }


    }

    @Override
    public void contextDestroyed(ServletContextEvent servletContextEvent) {
        System.out.println("web容器销毁");

    }
}
